package vip.xiaonuo.modular.cms.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import vip.xiaonuo.modular.cms.entity.Catalog;
import vip.xiaonuo.modular.cms.entity.Template;
import vip.xiaonuo.modular.cms.entity.Web;
import vip.xiaonuo.modular.cms.node.CatalogBaseTreeNode;
import vip.xiaonuo.modular.cms.param.CatalogParam;
import vip.xiaonuo.modular.cms.service.CatalogService;
import vip.xiaonuo.modular.cms.service.PublicApiService;
import vip.xiaonuo.modular.cms.service.TemplateService;
import vip.xiaonuo.modular.cms.service.WebService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: snowy_layui_cms
 * @Package: vip.xiaonuo.modular.cms.config
 * @ClassName: HttpServletInterceptor
 * @Author: liubaihan
 * @Description: 请求拦截器
 * @Date: 2022/6/27 17:40
 */
@Configuration
public class HttpServletInterceptor implements HandlerInterceptor {

    @Autowired
    private WebService webService;

    @Autowired
    private TemplateService templateService;
    
    @Autowired
    private CatalogService catalogService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取模板信息
        Template template = templateService.getOne(new LambdaQueryWrapper<Template>().eq(Template::getStatus, "1"));
        request.setAttribute("foldPath",template.getFolderPath());

        // 设置网站基础信息
        Web web = webService.list().get(0);

        // 获取栏目信息
        CatalogParam catalogParam = new CatalogParam();
        catalogParam.setIsView("Y");
        catalogParam.setIsMenu("Y");
        List<CatalogBaseTreeNode> catalogList = catalogService.tree(catalogParam);

        if(template.getFolderPath().equals("default")){
            request.setAttribute("foldPath",template.getFolderPath());
            request.setAttribute("title",web.getTitle());
            request.setAttribute("logo",web.getLogo());
            request.setAttribute("description", web.getDescription());
            request.setAttribute("copyRight",web.getCopyright());
            request.setAttribute("ICP",web.getIcp());
            request.setAttribute("subTitle",web.getSubtitle());
            request.setAttribute("catalogList",catalogList);
        }else{
            Map basicInfo = new HashMap<>();
            basicInfo.put("foldPath",template.getFolderPath());
            basicInfo.put("title",web.getTitle());
            basicInfo.put("logo",web.getLogo());
            basicInfo.put("description", web.getDescription());
            basicInfo.put("copyRight",web.getCopyright());
            basicInfo.put("ICP",web.getIcp());
            basicInfo.put("subTitle",web.getSubtitle());
            basicInfo.put("domain",web.getDomain());
            basicInfo.put("catalogList",catalogList);
            request.setAttribute("basicInfo",basicInfo);
        }
        request.setAttribute("domain",web.getDomain());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

}
